package ${package.ServiceImpl};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ${cfg.parent}.bean.*;
import ${cfg.parent}.dto.*;
import ${cfg.parent}.entity.*;
import ${cfg.parent}.mapper.*;
import ${cfg.parent}.service.*;
import ${cfg.parent}.utils.*;
import com.xc.core.bean.*;
import com.xc.core.dto.*;
import com.xc.core.exception.*;
import com.xc.core.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * ${cfg.describe}服务实现类
 * </p>
 *
 * @author ${author}
 * @version 1.0.0
 * @jdk 1.8.0
 * @since ${date}
 */
@Service
public class ${cfg.name?cap_first}ServiceImpl extends ServiceImpl<${cfg.name?cap_first}Mapper, ${cfg.name?cap_first}Entity> implements ${cfg.name?cap_first}Service {

    @Autowired
    private ${cfg.name?cap_first}Mapper ${cfg.name}Mapper;

    @Override
    @Transactional
    public ${cfg.name?cap_first}Dto create${cfg.name}(${cfg.name?cap_first}Bean ${cfg.name}Bean) {
        ${cfg.name?cap_first}Entity ${cfg.name}Entity = ObjectUtil.convert(new ${cfg.name?cap_first}Entity(), ${cfg.name}Bean);
        if (!this.retBool(${cfg.name}Mapper.insert(${cfg.name}Entity))) {
            throw new OperateException("u001", "添加失败！");
        }
        return ObjectUtil.convert(new ${cfg.name?cap_first}Dto(), ${cfg.name}Entity);
    }

    @Override
    public List<${cfg.name?cap_first}Dto> get${cfg.name}(QueryBean queryBean, ${cfg.name?cap_first}Bean ${cfg.name}Bean) {
        ${cfg.name?cap_first}Entity ${cfg.name}Entity = ObjectUtil.convert(new ${cfg.name?cap_first}Entity(), ${cfg.name}Bean);
        QueryWrapper<${cfg.name?cap_first}Entity> queryWrapper = ServiceUtils.queryData(queryBean, ${cfg.name}Entity);
        List<${cfg.name?cap_first}Entity> ${cfg.name}Entities = ${cfg.name}Mapper.selectList(queryWrapper);
        List<${cfg.name?cap_first}Dto> ${cfg.name}List = new ArrayList<>();
        for (${cfg.name?cap_first}Entity entity : ${cfg.name}Entities) {
            ${cfg.name}List.add(ObjectUtil.convert(new ${cfg.name?cap_first}Dto(), entity));
        }
        return ${cfg.name}List;
    }

    @Override
    public PagingDto get${cfg.name}Paging(PagingBean pagingBean, QueryBean queryBean, ${cfg.name?cap_first}Bean ${cfg.name}Bean) {
        ${cfg.name?cap_first}Entity ${cfg.name}Entity = ObjectUtil.convert(new ${cfg.name?cap_first}Entity(), ${cfg.name}Bean);
        QueryWrapper<${cfg.name?cap_first}Entity> queryWrapper = ServiceUtils.queryData(queryBean, ${cfg.name}Entity);
        IPage<${cfg.name?cap_first}Entity> page = ${cfg.name}Mapper.selectPage(new Page<>(pagingBean.getPage(), pagingBean.getSize()), queryWrapper);
        List<${cfg.name?cap_first}Dto> ${cfg.name}List = new ArrayList<>();
        for (${cfg.name?cap_first}Entity ${cfg.name} : page.getRecords()) {
            ${cfg.name}List.add(ObjectUtil.convert(new ${cfg.name?cap_first}Dto(), ${cfg.name}));
        }
        return new PagingDto<>(page.getTotal(), page.getSize(), ${cfg.name}List);
    }

    @Override
    public ${cfg.name?cap_first}Dto get${cfg.name}ById(String id) {
        return ObjectUtil.convert(new ${cfg.name?cap_first}Dto(), ${cfg.name}Mapper.selectById(id));
    }

    @Override
    public ${cfg.name?cap_first}Dto update${cfg.name}(String id, ${cfg.name?cap_first}Bean ${cfg.name}Bean) {
        ${cfg.name?cap_first}Entity ${cfg.name}Entity = ObjectUtil.convert(new ${cfg.name?cap_first}Entity(), ${cfg.name}Bean);
        ${cfg.name}Entity.setId(id);
        if (!this.retBool(${cfg.name}Mapper.updateById(${cfg.name}Entity))) {
            throw new OperateException("u002", "修改失败！");
        }
        return ObjectUtil.convert(new ${cfg.name?cap_first}Dto(), ${cfg.name}Entity);
    }

    @Override
    public void delete${cfg.name}(String id) {
        if (!this.retBool(${cfg.name}Mapper.deleteById(id))) {
            throw new OperateException("u003", "删除失败！");
        }
    }
}
